package ${package.Controller};

import lombok.extern.slf4j.Slf4j;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import ${cfg.parent}.common.controller.BaseController;
import ${package.Entity}.${entity};
import ${cfg.parent}.common.model.BaseResp;
import ${package.Service}.I${entity}Service;
import ${cfg.parent}.common.utils.ResponseUtils;
import org.springframework.ui.Model;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Arrays;
import java.util.List;

@Slf4j
@Controller
@RequestMapping("/${cfg.modelName}/${table.entityPath}")
public class ${entity}Controller extends BaseController {

	@Autowired
	private I${entity}Service ${table.entityPath}ServiceImpl;


	/**
	* 添加记录
	*/
	@RequestMapping(value = "add")
	@ResponseBody
	public Object add(@ModelAttribute("command") ${entity} command) throws Exception {
		BaseResp resp = new BaseResp();
		boolean result= ${table.entityPath}ServiceImpl.add(command);
		resp.setData(result);
		return resp;
	}

	/**
	* 修改记录
	*/
	@RequestMapping(value = "modify")
	@ResponseBody
	public Object modify(@ModelAttribute("command") ${entity} command) throws Exception {
		BaseResp resp = new BaseResp();
		boolean result= ${table.entityPath}ServiceImpl.modify(command);
		resp.setData(result);
		return resp;
	}

	/**
	* 根据主键指删除记录
	*/
	@RequestMapping(value = "delete")
	@ResponseBody
	public Object delete() throws Exception {
		BaseResp resp = new BaseResp();
		String ids = request.getParameter("ids");
		boolean result = ${table.entityPath}ServiceImpl.removeByIds(Arrays.asList(ids.split(",")));
		resp.setData(result);
		return resp;
	}

	/**
	* 根据主键获取记录信息
	*/
	@RequestMapping(value = "queryById")
	@ResponseBody
	public Object queryById(@ModelAttribute("command") ${entity} command) throws Exception {
		BaseResp resp = new BaseResp();
		${entity} record = ${table.entityPath}ServiceImpl.getById(command.getId());
		resp.setData(record);
		return resp;
	}

	/**
	* 查询记录信息
	*/
	@RequestMapping(value = "query")
	@ResponseBody
	public Object query(@ModelAttribute("command") ${entity} command) throws Exception {
		if (command == null) {
			command = new ${entity}();
		}
		Page page = ${table.entityPath}ServiceImpl.query(command);
		return ResponseUtils.pagePlusResp(page);
	}

	/**
	* 获取所有记录信息，不分页
	*/
	@RequestMapping(value = "queryAll")
	@ResponseBody
	public Object queryAll(@ModelAttribute("command") ${entity} command) throws Exception {
		BaseResp resp = new BaseResp();
		if (command == null) {
			command = new ${entity}();
		}
		List<${entity}> records = ${table.entityPath}ServiceImpl.queryAll(command);
    	resp.setData(records);
		return resp;
	}

}